<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        /**
         * Target 继承者
         * Origin 继承源
         */ 
        function inherit(Target,Origin){
            function Buffer(){}
            Buffer.prototype = Origin.prototype;
            Target.prototype = new Buffer();
            Target.prototype.constructor = Target; // 指定构造器始终指向自己
            Target.prototype.super_class = Origin; // 指定继承源
        }
        function Teacher(){
            this.name = "Teacher";
            this.age = 20;
        }
        function Student(){
            this.name = "Student";
        }
        inherit(Student,Teacher);

        Student.prototype.aaa = "admin";
        let t = new Teacher();
        let s = new Student();
        console.log(t);
        console.log(s);

        function Test(){
            let num = 0;
            this.add = function(){
                num++;
                console.log(num);
            }
            this.sub = function(){
                num--;
                console.log(num);
            }
            // return this; 默认return this
            // return 123 null undefined; 无效 还是默认return this
            // return []; 会被修改 直接return []
            // return undefined;
        }
        /**
         *  构造函数在使用new关键字时会默认return this 形成闭包
         *  手动return 原始值无效
         *  手动return 引用值生效
         */
        let test = new Test();
        test.add();
        test.add();
        test.add();
        test.add();
        test.sub();


        // 使用闭包优化 继承封装
        let inherit = (function(){
            let Buffer = function () {}
            return function(Target,Origin){
                Buffer.prototype = Origin.prototype;
                Target.prototype = new Buffer();
                Target.prototype.constructor = Target;
                Target.prototype.super_class = Origin;
            }
        })();
    </script>
</body>
</html>